import {Component, Input, OnInit} from '@angular/core';
import {AppGlobalService} from '../../shared/service/app-global.service';

@Component({
    selector: 'app-choose-type',
    templateUrl: './choose-type.component.html',
    styleUrls: ['./choose-type.component.scss']
})
export class ChooseTypeComponent implements OnInit {

    @Input() tagData: any;
    aWidth: string;        // 控制每个按钮宽度
    types: any[];
    sessionId: string;

    constructor(
        private appGlobalService: AppGlobalService
    ) {
    }

    ngOnInit() {
        this.types = [];
        console.log(this.tagData);
        this.dealData(this.tagData);
        this.sessionId = localStorage.getItem('sessionId');
    }

    dealData(data) {
        let self = this;
        if (data.columnCount) {
            this.aWidth = 100 / (data.columnCount + 1) + '%';
        }
        let index = 0;
        this.types[index] = [];
        circle(data.choiceButton);

        function circle(inform) {
            if (inform.length > data.columnCount) {
                self.types[index] = inform.slice(0, data.columnCount);
                ++index;
                circle(inform.slice(data.columnCount));
            } else {
                self.types[index] = inform;
            }
        }
    }

    /**
     * 跳转
     * @param obj
     */
    goto(obj) {
        let pageParams = {
            pageId: obj.target,
            params: {},
            sessionId: this.sessionId
        };
        this.appGlobalService.goToForward(pageParams)
    }
}
